
import router from '@/router'
import nProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store'
const whitelist = ['/login', '/404']
// 前置守卫
router.beforeEach(async(to, from, next) => {
  nProgress.start()
  // 如果有token
  if (store.getters.token) {
    // 有token去登录页，转到主页
    if (to.path === '/login') {
      next('/')
      nProgress.done()
    } else {
      // 不去登录页，放行
      if (store.getters.userId) {
        // 有用户资料，放行
        next()
      } else {
        // 无用户资料，获取
        await store.dispatch('user/getUserProfileAction')
        next()
      }
    }
  } else {
    // 没有token的情况，去白名单页面,放行
    if (whitelist.includes(to.path)) {
      next()
    } else {
      next('/login')
      nProgress.done()
    }
  }
})

// 后置守卫
router.afterEach((to, from, next) => {
  nProgress.done()
})
